/*
Copyright (c) 2010 J�r�my "Capitaine Mousse" barbe (jeremybarbe@capitaine-mousse.fr)

SnippIt is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.

You should have received a copy of the GNU Affero General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

$(function(){

    var edit = null;

    
    var db = getDB();
    var req = db.execute('SELECT id_categorie, nom_categorie FROM table_categorie ORDER BY nom_categorie');

    if(Titanium.UI.currentWindow.idcode != undefined){
        edit = db.execute("SELECT table_snippet.nom_snippet, table_snippet.code_snippet, table_snippet.code_langue, table_categorie.id_categorie FROM table_snippet LEFT JOIN table_cat_to_snippet ON table_cat_to_snippet.id_snippet = table_snippet.id_snippet LEFT JOIN table_categorie ON table_categorie.id_categorie = table_cat_to_snippet.id_categorie WHERE table_snippet.id_snippet = '"+Titanium.UI.currentWindow.idcode+"'");
    }

    while(req.isValidRow()){
        $('#categorie').append('<option value="'+req.fieldByName('id_categorie')+'">'+req.fieldByName('nom_categorie')+'</option>');
        req.next();
    }

    $(window).resize(function(){ size(); });

    function size(){
        $('#code').height($(window).height()-80);
        $('#code').width($(window).width()-10);
        $('#bottom').width($(window).width()-10);
    }

    size();

    $('#enregistrer').click(function(){
        var nom = addslashes($('#nom').val());
        var code = htmlspecialchars(addslashes($('#code').val()));
        var cat = addslashes($('#categorie').val());
        var lang = addslashes($('#langage option:selected').text());
        var codlang = addslashes($('#langage').val());

        if(nom != "" && code != ""){
            if(edit)
                db.execute('UPDATE table_snippet SET nom_snippet = "'+nom+'", code_snippet = "'+code+'", langage_snippet = "'+lang+'", code_langue = "'+codlang+'" WHERE id_snippet ="'+Titanium.UI.currentWindow.idcode+'"');
            else
                db.execute('INSERT INTO table_snippet(id_snippet, nom_snippet, code_snippet, langage_snippet, code_langue) VALUES(NULL, "'+nom+'", "'+code+'", "'+lang+'", "'+codlang+'")');

            if(cat != 'none'){
                var id;

                if(edit)
                    id = edit;
                else{
                    req = db.execute('SELECT last_insert_rowid() AS id');
                    id = req.fieldByName('id');
                }

                db.execute('DELETE FROM table_cat_to_snippet WHERE id_snippet = "'+id+'"');
                db.execute('INSERT INTO table_cat_to_snippet VALUES(NULL, "'+id+'", "'+cat+'")');
            }

            Titanium.UI.getCurrentWindow().close();
        }
        
        return false;
    });

    if(edit != null){
        $('#nom').val(stripslashes(edit.fieldByName('nom_snippet')));
        $('#code').val(htmlspecialchars_decode(stripslashes(edit.fieldByName('code_snippet'))));
        $('#langage option[value="'+stripslashes(edit.fieldByName('code_langue'))+'"]').attr('selected', 'selected');
        $('#categorie option[value="'+stripslashes(edit.fieldByName('id_categorie'))+'"]').attr('selected', 'selected')
    }


    $('#code').tabby();
});